POV-Ray : Newsgroups : povray.general : bent_tube macro : bent_tube macro Server Time
1 Aug 2024 10:16:47 EDT (-0400)
  bent_tube macro  
From: wilhelm
Date: 24 Nov 2005 09:50:00
Message: <web.4385d1fa6cdd00c2b3bc7f70@news.povray.org>
I needed to draw bent tubing. I couldn't find a ready made macro, so I wrote
my first one. Here it is in case anyone can use it. I used Friedrich A.

Segment_of_Torus macro as part of this.



global_settings {
   assumed_gamma 2.2            // for most PC monitors
   max_trace_level 9
   ambient_light <1, 1, 1>
}


light_source  {
   <0, 80, 0>
   color  <1, 1, 1>
}

background { color <.5, .5, .8> }

/***************************************************************
   friedrich loehmueller's "segment_of_torus" macro
***************************************************************/

//--------------------------------------------------------Segment_of_Torus
macro
#macro Segment_of_Torus ( R_major, R_minor, Segment_Angle)
//---------------------------------------------------------------------------
#local D =  0.00001;
 #if (Segment_Angle < 0)
      #local Negativ_Flag = 1;
      #local Segment_Angle = -Segment_Angle;
 #else
      #local Negativ_Flag = 0;
 #end
 #if (Segment_Angle > 360) #local Segment_Angle = mod(Segment_Angle,360);
#end

intersection{
 torus { R_major, R_minor }

#if (Segment_Angle > 180)
 union{
#end // use union!

 box   { <-1,-1,0>,<1,1,1>
         scale < R_major+R_minor+D, R_minor+D, R_major+R_minor+D>
       }// end of box
 box   { <-1,-1,-1>,<1,1,0>
         scale < R_major+R_minor+D, R_minor+D, R_major+R_minor+D>
         rotate < 0,-Segment_Angle,0 >
       }// end of box

#if (Segment_Angle > 180)
 }
#end // end of union, if union is used!

 #if (Negativ_Flag = 0)  rotate<0, Segment_Angle,0>   #end

 } // end of intersection
#end  // ----------------------------- end of macro Segment_of_Torus


/**********************************************************************

**********************************************************************/

#declare AXT = .1;            // axis thickness
#declare AXL = 20;            // axis length
#declare AHL = .1*AXL;         // arrow head length
#declare AHW = 4*AXT;            // arrow head width

#ifndef (AXT)
   #declare AXT = .05;               // axis thickness
#end

#ifndef (AXL)
   #declare AXL = 10;               // axis length
#end

#ifndef (AHL)
   #declare AHL = .09*AXL;         // arrow head length
#end

#ifndef (AHW)
   #declare AHW = .04*AXL;         // arrow head width
#end

#declare axes = union {

   // x axis:

   cylinder {
     -AXL*x,  AXL*x,  AXT
      pigment { color <1, 0, 0> }
       finish {
           ambient 0.75
         diffuse 0.7
         reflection 0.15
         brilliance 8
         specular 0.8
         roughness 0.1
      }
   }


   cone  {
      <AXL+AHL, 0, 0>,  0.0,
      <AXL, 0, 0>, AHW
      pigment {color <1, 0, 0>}

       finish {
           ambient 0.75
         diffuse 0.7
         reflection 0.15
         brilliance 8
         specular 0.8
         roughness 0.1
      }

   }

// y axis

   cylinder {
     -AXL*y,  AXL*y,  AXT
     pigment { color <0, 1, 0> }
       finish {
           ambient 0.75
         diffuse 0.7
         reflection 0.15
         brilliance 8
         specular 0.8
         roughness 0.1
      }
   }

   cone  {
      <0, AXL+AHL, 0>,  0.0,
      <0, AXL, 0>, AHW
      pigment {color <0, 1, 0>}
       finish {
           ambient 0.75
         diffuse 0.7
         reflection 0.15
         brilliance 8
         specular 0.8
         roughness 0.1
      }
   }


// z axis:

   cylinder {
     -AXL*z,  AXL*z,  AXT
     pigment { color <0, 0, 1> }
       finish {
           ambient 0.75
         diffuse 0.7
         reflection 0.15
         brilliance 8
         specular 0.8
         roughness 0.1
      }
   }


   cone  {
      <0, 0, AXL+AHL>,  0.0,
      <0, 0, AXL>, AHW
      pigment {color <0, 0, 1>}
       finish {
           ambient 0.75
         diffuse 0.7
         reflection 0.15
         brilliance 8
         specular 0.8
         roughness 0.1
      }
   }


}               // axes



/***************************************************************

***************************************************************/

#macro bent_tube(len1, bend_radius, tube_radius, bend_angle, len2)

   #declare x1 = bend_radius*cos(bend_angle * (pi/180.0)) ;
   #declare z1 = -bend_radius*sin(bend_angle * (pi/180.0)) ;

   #declare m1 = z1 / x1;          // slope of line from <0,0> to <x1,z1>
   #declare m2 = -1/m1;

   #declare x2 = x1 - len2 / sqrt(1 + m2*m2);
   #declare z2 = z1 - (m2*len2) / sqrt(1 + m2*m2);

   #declare x3 = x1 - (len2+.1) / sqrt(1 + m2*m2);
   #declare z3 = z1 - (m2*(len2+.1)) / sqrt(1 + m2*m2);

   merge  {
      object  {
         Segment_of_Torus(bend_radius, tube_radius, bend_angle)
         #ifdef (dbg)
            pigment { rgb <0, .5, 0> }
            finish {
               ambient 0.75
               diffuse 0.7
               roughness 0.1
            }
         #end
      }

      #if (len1>0)
         object  {
            difference  {
               cylinder {
                  <bend_radius, 0, 0>,            // end 1
                  <bend_radius, 0, len1>,         // end 2
                  tube_radius                  // radius
                  //open
               }

               cylinder {
                  <bend_radius, 0, 0>,            // end 1
                  <bend_radius, 0, len1+.1>,         // end 2
                  .9*tube_radius                  // radius
                  //open
               }
            }
            #ifdef (dbg)
               pigment { rgb <.5, 0, 0> }
               finish {
                  ambient 0.75
                  diffuse 0.7
                  roughness 0.1
               }
            #end
         }
      #end

      #if (len2>0)
         object  {
            difference  {
               cylinder {
                  <x1, 0, z1>,
                  <x2, 0, z2>,
                  tube_radius
                  //open
               }
               cylinder {
                  <x1, 0, z1>,
                  <x3, 0, z3>,
                  .9*tube_radius
                  //open
               }
            }

            #ifdef (dbg)
               pigment { rgb <0, 0, 1> }
               finish {
                  ambient 0.75
                  diffuse 0.7
                  roughness 0.1
               }
            #end
         }
      #end

      //sphere { <x1, 0, z1>, .5 pigment {rgb<1,0,0>} }

      rotate<0, 90, 0>
      rotate<0, 0, 180>
      translate<len1, 0, bend_radius>

   }

#end


/**********************************************************************
   the scene
**********************************************************************/

camera  {
   location <10, 50, -10>
   look_at <0 , 0 , 0>
   blur_samples 20
}

object  { axes }

//#declare dbg=1;  // used by bent_tube to show the segments in different
colors

merge  {

   object  {
      bent_tube(10, 8, 1.5, 60, 15)
   }
   object  {
      bent_tube(10, 4, 1.5, 60, 8)
      rotate <0,0,180>
      rotate <-120, 0, 0>
   }
   pigment { rgb <.65, .57, 0> }
   finish {
      ambient 0.75
      diffuse 0.7
      roughness 0.1
   }
   rotate <0, 0, 60>
   rotate <-60,0,0>
   //translate <-5, 0, 5>

}


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.